package demo3;

public class Text07 {
    public static void main(String[] args) throws InterruptedException {
        Counter05  counter = new Counter05();
        Counter05  counter1 = new Counter05();
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                counter.increase();
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                counter1.increase();
            }
        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        System.out.println("count：" + counter.count);
    }
}


class Counter07 {
    // 初始值为0
    public static int count = 0;


    /**
     * 累加方法
     *
     */

    public void increase () {
        // 只定义锁代码块
        synchronized (String.class) {
            count++;
        }
    }
}
